package com.wee.aircoach_user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bigtotoro.util.SharedPreferencesUtil;
import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chatuidemo.DemoHXSDKHelper;
import com.easemob.chatuidemo.activity.ChatActivity;
import com.easemob.chatuidemo.utils.UserUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wee.entity.MD5Util;
import com.wee.entity.User_detail;

/**
 * 提问界面
 */
public class ConsultPostActivity extends Activity implements View.OnClickListener {

    private User_detail user;
    private int recipientType = 0;

    private ImageView stateImg0, stateImg1;
    private EditText editContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consult_post);
        initView();
    }

    private void initView() {
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.item0).setOnClickListener(this);
        findViewById(R.id.item1).setOnClickListener(this);
        findViewById(R.id.btn_right).setOnClickListener(this);

        stateImg0 = (ImageView)findViewById(R.id.item0_state);
        stateImg1 = (ImageView)findViewById(R.id.item1_state);
        editContent = (EditText)findViewById(R.id.content);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.item0:
                recipientType = 0;
                stateImg0.setVisibility(View.VISIBLE);
                stateImg1.setVisibility(View.INVISIBLE);
                break;
            case R.id.item1:
                recipientType = 1;
                stateImg0.setVisibility(View.INVISIBLE);
                stateImg1.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_right:
                String content = editContent.getText().toString();
                if(content.length() == 0){
                    Toast.makeText(ConsultPostActivity.this, "提问内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
              gethttp();
                break;
        }
    }
    private void chat(User_detail user) {
        if(user==null){
            return;
        }
        final String content = editContent.getText().toString();
        final String pwd = "abc123";
        final String coach = "coach_"+user.getCoach().getId();
        final String user1 = "user_"+user.getId();

        UserUtils.getUserInfo(coach).setNick(user.getCoach().getName());
        UserUtils.getUserInfo(coach).setAvatar(user.getCoach().getFigure());
        if (DemoHXSDKHelper.getInstance().isLogined()) {//进入主页面
            Intent intent = new Intent(this, ChatActivity.class);
             intent.putExtra("userId", coach);
            intent.putExtra("text", content);
            startActivity(intent);
            finish();
            return;
        }else {
            EMChatManager.getInstance().login(user1, pwd, new EMCallBack() {
                @Override
                public void onSuccess() {
                    Intent intent = new Intent(ConsultPostActivity.this, ChatActivity.class);
                    intent.putExtra("userId", coach);
                    intent.putExtra("text", content);
                    startActivity(intent);
                    finish();
                }

                @Override
                public void onProgress(int arg0, String arg1) {
                    // TODO Auto-generated method stub
                }
                @Override
                public void onError(int arg0, String arg1) {
                    // TODO Auto-generated method stub

                }
            });
        }
    }


    /** 解析字段 */
    public void gethttp() {
        int  exact=  SharedPreferencesUtil.getInt(this, com.wee.aircoach_user.Constant.USERTIME);
        int  it=  SharedPreferencesUtil.getInt(ConsultPostActivity.this,Constant.USERID);
         String  user_main=Constant.USER_DETAIL+ MD5Util.md5(exact)+"&id="+it+"";
         HttpUtils httpUtils = new HttpUtils();
         httpUtils.send(HttpRequest.HttpMethod.GET, user_main,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {

                        String str = responseInfo.result;
                        Gson gs = new Gson();
                        JsonObject jo = gs.fromJson(str, JsonObject.class);
                        JsonElement je = jo.get("status");
                        String status = je.getAsString();
                        if ("ok".equals(status)) {
                            JsonObject object = jo.getAsJsonObject("data");
                            try {
                                user = gs.fromJson(object, User_detail.class);
                                try{
                                    chat(user);
                                }catch (Exception e ){
                                }
                            } catch (Exception r) {
                            }
                                

                        }

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        System.out.println(e.getMessage());
                    }
                });

    }

}
